package com.huang.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//lock   可重入锁  可多次拿到锁
public class Demo2 {
    public static void main(String[] args) {
        Phone2 phone = new Phone2();
        new Thread(()->{
            phone.sms();
        },"A").start();
        new Thread(()->{
            phone.sms();
        },"B").start();
    }
    }

class Phone2{
       Lock lock= new ReentrantLock();
    public synchronized void  call(){
        lock.lock();

        try {
            System.out.println(Thread.currentThread().getName()+"call---");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public synchronized void  sms(){
        lock.lock();  //锁 必须成对
        try {
            System.out.println(Thread.currentThread().getName()+"sms---");
            call();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}

